﻿using JzSport.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace JzSport.Api
{
    /// <summary>
    /// 获取消息集列表
    /// </summary>
    class GetMessageClipListAction : AuthGetAction<Params.GetMessageClipListParams,
        Results.GetMessageClipListResult>
    {
        protected override ActionResult<Results.GetMessageClipListResult> OnExecute(ActionContext context, 
            SysParameters sysParameters,
            Params.GetMessageClipListParams opParams)
        {
            int limit = 100;
            int start = opParams.Page * limit;
            ServiceResult<List<Model.Data.MessageClipView>> serviceResult = new MessageService().GetMessageClipListByType(context.AccountId,
                opParams.Type,
                start,
                limit);

            Results.GetMessageClipListResult actionResult = new Results.GetMessageClipListResult();

            if (serviceResult.ErrorCode == ErrorCode.NoError)
            {
                for (int index = 0; index < serviceResult.Data.Count; index++)
                {
                    Results.GetMessageClipListResult.MessageSetItem item = new Results.GetMessageClipListResult.MessageSetItem();

                    item.Id = serviceResult.Data[index].Id;
                    item.CreatedTime = serviceResult.Data[index].CreatedTime;
                    item.LastestContent = serviceResult.Data[index].LastestContent;
                    item.LastestSenderId = serviceResult.Data[index].LastestSenderId;
                    item.LastestTitle = serviceResult.Data[index].LastestTitle;
                    item.LastestSenderAvatarUrl = String.Format("{0}{1}", Configs.BaseWebUrl, serviceResult.Data[index].LastestSenderAvatarUrl);
                    item.LastestSenderNickName = serviceResult.Data[index].LastestSenderNickName;
                    item.LastestUpdatedTime = serviceResult.Data[index].LastestUpdatedTime;
                    item.MessageCount = serviceResult.Data[index].MessageCount;
                    item.NewMessageCount = serviceResult.Data[index].NewMessageCount;
                    item.ReceiverId = serviceResult.Data[index].ReceiverId;
                    item.Type = serviceResult.Data[index].Type;
                    item.MessageType = serviceResult.Data[index].MessageType;
                    item.Tag = serviceResult.Data[index].Tag;

                    actionResult.Add(item);
                }

                return new ActionResult<Results.GetMessageClipListResult>("1.0.2", true, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
            }
            return new ActionResult<Results.GetMessageClipListResult>("1.0.2", false, serviceResult.ErrorCode,
                    ErrorCode.ToString(serviceResult.ErrorCode), actionResult);
        }
    }
}
